<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="下单用户ID" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="下单用户ID"></el-input>
    </el-form-item>
    <el-form-item label="商品ID" prop="goodsId">
      <el-input v-model="dataForm.goodsId" placeholder="商品ID"></el-input>
    </el-form-item>
    <el-form-item label="购买数量" prop="quantity">
      <el-input v-model="dataForm.quantity" placeholder="购买数量"></el-input>
    </el-form-item>
    <el-form-item label="订单总价" prop="totalPrice">
      <el-input v-model="dataForm.totalPrice" placeholder="订单总价"></el-input>
    </el-form-item>
    <el-form-item label="支付状态:0未付款,1已付款" prop="payStatus">
      <el-input v-model="dataForm.payStatus" placeholder="支付状态:0未付款,1已付款"></el-input>
    </el-form-item>
    <el-form-item label="付款时间" prop="payTime">
      <el-input v-model="dataForm.payTime" placeholder="付款时间"></el-input>
    </el-form-item>
    <el-form-item label="下单时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="下单时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          goodsId: '',
          quantity: '',
          totalPrice: '',
          payStatus: '',
          payTime: '',
          createTime: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '下单用户ID不能为空', trigger: 'blur' }
          ],
          goodsId: [
            { required: true, message: '商品ID不能为空', trigger: 'blur' }
          ],
          quantity: [
            { required: true, message: '购买数量不能为空', trigger: 'blur' }
          ],
          totalPrice: [
            { required: true, message: '订单总价不能为空', trigger: 'blur' }
          ],
          payStatus: [
            { required: true, message: '支付状态:0未付款,1已付款不能为空', trigger: 'blur' }
          ],
          payTime: [
            { required: true, message: '付款时间不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '下单时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/bbm/goodsorder/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.goodsOrder.userId
                this.dataForm.goodsId = data.goodsOrder.goodsId
                this.dataForm.quantity = data.goodsOrder.quantity
                this.dataForm.totalPrice = data.goodsOrder.totalPrice
                this.dataForm.payStatus = data.goodsOrder.payStatus
                this.dataForm.payTime = data.goodsOrder.payTime
                this.dataForm.createTime = data.goodsOrder.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/bbm/goodsorder/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'goodsId': this.dataForm.goodsId,
                'quantity': this.dataForm.quantity,
                'totalPrice': this.dataForm.totalPrice,
                'payStatus': this.dataForm.payStatus,
                'payTime': this.dataForm.payTime,
                'createTime': this.dataForm.createTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
